package com.sheep.util;

import com.sheep.api.bean.ADSBBean;
import org.apache.hadoop.io.Text;

import javax.json.Json;
import javax.json.JsonBuilderFactory;

/**
 * Created by Administrator on 2015/9/6.
 */
public class JsonSupport {
    private static final JsonBuilderFactory bf = Json.createBuilderFactory(null);
    public static String SIGN = "#";

    public static String convertASDB2JSON(ADSBBean bean) {
        return bean.getAn() + SIGN + bean.getFi() + " " + (bf.createObjectBuilder()
                .add("lat", bean.getLat())
                .add("lon", bean.getLon())
                .add("cas", bean.getCas())
                .add("vec", bean.getVec())
                .add("time", bean.getTime()).build().toString());
    }

    /**
     * 整合结果到一个JSON中
     */
    public static String getTotalJSON(Iterable<Text> values) {
        String temp = "";
        for (Text i : values) {
            temp = i.toString() + ",\n" + temp;
        }
        if (temp.endsWith(",")) {
            temp = temp.trim().substring(0, temp.length() - 1);
        }
        temp = "{\"ADSB\": [\n" + temp + "]\n}";
        return temp;
    }

    public static void main(String[] args) {
    }
}
